package com.example.maple.ccsclient.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.adapter.GridViewAdapter;
import com.example.maple.ccsclient.adapter.RecyclerViewAdapter;
import com.example.maple.ccsclient.instance.DataInfoInstance;
import com.example.maple.ccsclient.interfaces.ISearchView;
import com.example.maple.ccsclient.model.PicModel;
import com.example.maple.ccsclient.presenter.SearchPresenter;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;
import com.example.maple.ccsclient.view.OnItemClickListener;
import com.example.maple.ccsclient.view.SpaceItemDecoration;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends Activity implements ISearchView {

	private ImageButton mImageButton;
	private Animation anim;

	RecyclerView mRecyclerView = null;
	RecyclerViewAdapter mRecyclerViewAdapter = null;

	OnScrollStateChanged mOnScrollStateChanged = null;
	List<PicModel> picList = null;

	GridViewAdapter mGridViewAdapter = null;
	SearchPresenter mSearchPresenter = null;
	private int mColumn;
	String info = null;
	private Context mContext;


	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		Fresco.initialize(this);
		setContentView(R.layout.activity_search);
		mContext = this;
		mColumn = SharedPreferencesUtils.getPicColumn(mContext);
		findView();

	}


	private void findView() {
		Intent intent = getIntent();
		info = intent.getStringExtra("info");
		findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mImageButton = (ImageButton) findViewById(R.id.refresh);
		anim = AnimationUtils.loadAnimation(SearchActivity.this, R.anim.refresh_rotate);
		mImageButton.startAnimation(anim);
		anim.cancel();
		mImageButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//刷新数据
				mSearchPresenter.refresh(info);
			}
		});
		mSearchPresenter = new SearchPresenter(this, this);
		picList = new ArrayList<>();
		mGridViewAdapter = new GridViewAdapter(this, R.layout.iamge_item, picList, mColumn);

		mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
		mRecyclerView.setLayoutManager(new GridLayoutManager(this, mColumn));
		//间距
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.addItemDecoration(new SpaceItemDecoration(this, 3));
		mRecyclerViewAdapter = new RecyclerViewAdapter(SearchActivity.this, R.layout.iamge_item, picList, mColumn);
		mRecyclerView.setAdapter(mRecyclerViewAdapter);
		mRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(View view, int position) {
				PicModel info = picList.get(position);
				DataInfoInstance.getInstance().setPicModels(picList);
				PicDetailActivity.startAction(mContext,position);
			}
		});
		mOnScrollStateChanged = new OnScrollStateChanged();
		//获取数据
		mSearchPresenter.refresh(info);

	}

	@Override
	protected void onResume() {
		super.onResume();
		mRecyclerView.addOnScrollListener(mOnScrollStateChanged);

	}


	public static void startAction(Context context, String info) {
		Intent intent = new Intent(context, SearchActivity.class);
		intent.putExtra("info", info);
		context.startActivity(intent);
	}

	//显示通过输入文字查询出来的图片
	@Override
	public void showSearchPic(List<PicModel> list) {

		picList.clear();
		picList.addAll(list);
		//mGridViewAdapter.notifyDataSetChanged();
		mRecyclerViewAdapter.notifyDataSetChanged();
	}

	//开始动画
	@Override
	public void startAnimation() {
		if (null != anim) {
			anim.start();
		}

	}

	//停止动画
	@Override
	public void stopAnimation() {
		if (null != anim) {
			anim.cancel();
		}
	}


	@Override
	public void showDialog(String toastText) {
		Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
	}


	@Override
	protected void onPause() {
		super.onPause();
		if (null != anim) {
			anim.cancel();
		}

		mRecyclerView.removeOnScrollListener(mOnScrollStateChanged);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != anim) {
			anim.cancel();
		}
	}


	class OnScrollStateChanged extends RecyclerView.OnScrollListener {

		@Override
		public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
			super.onScrollStateChanged(recyclerView, newState);

			if (mRecyclerViewAdapter != null) {
				if (0 == newState) {
					//		mRecyclerViewAdapter.setScrollState(false);
				} else {
					//	mRecyclerViewAdapter.setScrollState(true);
				}
			}

		}
	}

}
